package com.infore.dataReceiving.core;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.infore.dataReceiving.core.parse.InstructionsParse;
import com.infore.dataReceiving.core.validation.InstructionsValidation;
import com.infore.dataReceiving.dto.AirInstructionsDto;
import com.infore.dataReceiving.netty.client.NettyClient;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;
import com.infore.dataReceiving.util.RedisUtil;
import com.infore.dataReceiving.util.ResultEntity;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;

@Component("air212InstructionsHandle")
public class Air212InstructionsHandle implements InstructionsHandle{
	
	private static final Logger log = LoggerFactory.getLogger(Air212InstructionsHandle.class);  
	
	@Autowired 
	@Qualifier("air212InstructionsValidation")
	private InstructionsValidation instructionsValidation;
	
	@Autowired 
	@Qualifier("air212InstructionsParse")
	private InstructionsParse instructionsParse;
	
	@Autowired
    private RedisUtil redisUtil;
	
	
	private static String ycip = null;
	static {
		try {
//			ycip = InetAddress.getLocalHost().getHostAddress();
			ycip = Air212InstructionsHandleUtil.getInetAddress("eth3", 4);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity handle(String instruction,ChannelHandlerContext ctx) {
		//指令验证
		ResultEntity resultEntity = instructionsValidation.validation(instruction);
		if(resultEntity.getCode()!=4) {
			System.out.println(resultEntity.getMessage());
			log.error(resultEntity.getMessage()+instruction);  
			return resultEntity;
		}
		//指令解析
		AirInstructionsDto airInstructionsDto = (AirInstructionsDto)((HashMap<String, Object>)resultEntity.getContent()).get("info");
		String mn = airInstructionsDto.getMn();
		ChannelHandlerContext ctx1 = Air212InstructionsHandleUtil.cMap.get("air_"+mn);
		if(ctx1==null) {
			Air212InstructionsHandleUtil.cMap.put("air_"+mn, ctx);
			Air212InstructionsHandleUtil.channelMap.put(ctx.channel().id().asLongText(), airInstructionsDto.getMn());
		}else if(ctx1!=null&&!ctx1.equals(ctx)) {
			Air212InstructionsHandleUtil.cMap.put("air_"+mn, ctx);
			Air212InstructionsHandleUtil.channelMap.put(ctx.channel().id().asLongText(), airInstructionsDto.getMn());
			ctx1.close();
		}
		if(Air212InstructionsHandleUtil.czMap.get(mn)==null) {
			Air212InstructionsHandleUtil.czMap.put(mn, "1");
		}
		resultEntity = instructionsParse.parse(airInstructionsDto, ctx);
		
//		System.out.println("本机ip地址为11111111111111111111111111111111111："+ycip);
		
		if(!"10.33.50.69".equals(ycip)&&("LH001510200026".equals(mn)||"LH001510200027".equals(mn)||"LH001510200028".equals(mn)
    			||"LH001510200022".equals(mn)||"LH001510200023".equals(mn)||"LH001510200024".equals(mn))) {
			redisUtil.lSet("romoteData", instruction);
    	}
		return resultEntity;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		Air212InstructionsHandle handle = new Air212InstructionsHandle();
		String msg = "##0127QN=20190523174006843;ST=21;CN=2061;PW=739404;MN=A430100_2007;Flag=9;CP=&&DataTime=20180507080000;w01001-Avg=7.1,w01001-Flag=N&&4F40\r\n";
		handle.handle(msg, null);
	}

}
